package com.wx.mall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.vo.order.OrderReCauseVo;
import com.wx.mall.order.entity.ReturnCause;
import com.wx.mall.order.mapper.ReturnCauseMapper;
import com.wx.mall.order.service.IReturnCauseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.order.vo.ReturnCauseVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.prefs.BackingStoreException;

/**
 * <p>
 * 退款原因 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-04-13
 */
@Service
public class ReturnCauseServiceImpl extends ServiceImpl<ReturnCauseMapper, ReturnCause> implements IReturnCauseService {

    @Autowired
    private ReturnCauseMapper returnCauseMapper;

    /**
     * 分页查询
     * @param
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<ReturnCause> find(Integer pageNum, Integer pageSize,int seq) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<ReturnCause> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("seq");
        return this.returnCauseMapper.selectList(queryWrapper);
    }


    /**
     * 添加保存
     * @param
     */
    @Override
    public int addSaveList(ReturnCauseVo causeVo) {
        ReturnCause returns=new ReturnCause();
        BeanUtils.copyProperties(causeVo,returns);
        int result=returnCauseMapper.insert(returns);
        return result;
    }


    /**
     *修改编辑
     *
     */
    @Override
    public int updateList(ReturnCauseVo causeVo) {
        ReturnCause returnCause=new ReturnCause();
        returnCause.setCause(causeVo.getCause());
        returnCause.setSeq(causeVo.getSeq());
        returnCause.setStatus(causeVo.getStatus());
        BeanUtils.copyProperties(causeVo,returnCause);
        int dd=returnCauseMapper.updateById(returnCause);
        return dd;
    }


    /**
     * 根据ID单条删除
     * @param returnId
     * @return
     */
    @Override
    public int deleteList(Integer returnId) {
        boolean del=this.removeById(returnId);
        QueryWrapper<ReturnCause> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",returnId).orderByAsc("seq");
        boolean success=this.remove(queryWrapper);
        int result;
        if (del == true||success == true){
            result=1;
        }else {
            result=0;
        }
        return result;
    }

    /**
     * 根据id批量删除
     * @param
     * @return
     */
    @Override
    public int deleteALLList(OrderReCauseVo vo) {
        List<Integer> userLists=new ArrayList<>();
            for (Integer id : vo.getIds() ){
                ReturnCause returnCause=this.getById(id);
                userLists.add(returnCause.getId());
            }
        boolean returnCause=this.removeByIds(userLists);
        return returnCause?1:0;
    }
}
